package com.wavehi.request_mapping.web.controller;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author WaveHi
 * @Date 4/11/2021 下午 4:04
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@RestController
public class ParameterController {

    //http://localhost:8080/pathVariable/1/owner/Tom
    @RequestMapping("/pathVariable/{id}/owner/{username}")
    public HashMap<String, Long> usePathVariable(
            @PathVariable("id") Long id,
            @PathVariable("username") String username,
            @PathVariable HashMap<String, Long> hashMap) {
        HashMap<String, Long> stringLongHashMap = new HashMap<>(16);
        stringLongHashMap.put(username, id);
        System.out.println(hashMap);
        return stringLongHashMap;
    }

    //http://localhost:8080/requestHeader
    @RequestMapping("/requestHeader")
    public String useRequestHeader(
            @RequestHeader("User-Agent") String userAgent,
            @RequestHeader Map<String, String> headers) {
        System.out.println(headers);
        return userAgent;
    }

    //http://localhost:8080/requestParam?num=12&string=Tom&nums=1&nums=3
    @RequestMapping("/requestParam")
    public String useRequestParam(
            @RequestParam("num") Integer age,
            @RequestParam("string") String name,
            @RequestParam("nums") List<Integer> numbers,
            @RequestParam Map<String, String> map) {
        System.out.println(age);
        System.out.println(name);
        System.out.println(numbers);
        System.out.println(map);//相同取第一个
        return name;
    }

    @RequestMapping("/cookieValue")
    public String useCookieValue(
            @CookieValue("_gt") String value,
            @CookieValue("_gt") Cookie cookie,
            @CookieValue Map<String, String> map) {
        System.out.println(cookie);
        System.out.println(map);
        return value;
    }

    //post请求
    @RequestMapping("/requestBody")
    public String useRequestBody(
            @RequestBody String value) {
        return value;
    }

    //http://localhost:8080/forward
    @RequestMapping("/requestAttribute")
    public String useRequestAttribute(
            @RequestAttribute("username") String username,
            @RequestAttribute("password") String password) {
        System.out.println(username);
        System.out.println(password);
        return username;
    }

    //http://localhost:8080/matrixVariable/Ok;name=Tom;nums=1,2,3
    @RequestMapping("/matrixVariable/{path}")
    public String useMatrixVariable(
            @MatrixVariable(value = "name", pathVar = "path") String name,
            @MatrixVariable(value = "nums", pathVar = "path") List<Integer> nums) {
        System.out.println(nums);
        return name;
    }
}
